package com.fitness.front.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.flume.api.RpcClient;
import org.apache.flume.api.RpcClientFactory;
import org.apache.flume.event.EventBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.concurrent.*;

@Slf4j
@Configuration
public class CommonConfig {

    private static final long workerId = 1;
    private static final long dataCenterId = 1;

    @Bean
    public Snowflake snowflake(){
        return new Snowflake(workerId, dataCenterId);
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }


    private int corePoolSize = 2;
    private int maxPoolSize = 10;
    private int keepAliveTime = 10;
    private int queueSize = 10000;

    @Bean
    public Executor sendRateExecutor() {
        return new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(queueSize),
                r -> {
                    Thread thread = new Thread(r);
                    thread.setName("flume-" + thread.getId());
                    return thread;
                }
        );
    }
}
